package com.FH.organization.people;

import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.permission.member_role.MemberRoleResource;
import com.FH.permission.orgnization_menu.OrgnizationmenuResource;
import com.FH.permission.privilege_menu.PrivilegemenuResource;
import com.FH.permission.sys_menu.SysmenuShowDTO;
import com.FH.permission.sys_role_member.SysRoleMemberResource;
import com.FH.util.AccessDB;
import com.FH.util.FileProcess;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by jinweizhe on 16/6/23.
 */
@Stateless
public class KamMemberDAOImpl extends AccessDB implements KamMemberDAO {

    @Inject
    OperatorLogResource operatorLogResource;
    @Inject
    PrivilegemenuResource privilegemenuResource;
    @Inject
    OrgnizationmenuResource orgnizationmenuResource;

    @Inject
    MemberRoleResource memberRoleResource;

    @Inject
    DepartmentResource departmentResource;
    @Inject
    AttachmentResource attachmentResource;

    @Inject
    SysRoleMemberResource sysRoleMemberResource;

    /**
     * 登录获取用户信息方法
     *
     * @param member_name
     * @return
     */
    @Override
    public ResultDTO getLoginInfo(String member_name, String password) {
        ResultDTO resultDTO = new ResultDTO();
        String existName = "0";

        //判断用户名是否存在
        if (null != member_name && !"".equals(member_name)) {

            String sqlexit = "select count(1) from kam_member where nickname = '" + member_name + "'";

            existName = getReportingToOne(sqlexit);
        }

//        String pwd = MD5.md5(password);


        List<String[]> seresult = new ArrayList<String[]>();

        List<KamMemberShowDTO> result = new ArrayList<KamMemberShowDTO>();

        if (!existName.equals("0")) {

            //登录sql语句
            String sql = "select k.member_id,k.member_code,k.nickname,k.member_name,k.member_password,k.member_gender," +
                    "k.member_birthday,k.member_phone,k.member_id_card,k.PROVINCE_ID,p.PROVINCE_NAME," +
                    "k.CITY_ID,c.CITY_NAME,k.organization_id,o.organization_name,h.head_portrait_add,k.REGISTER_DATE,k.UP_DATE," +
                    "k.member_email,k.vacationyeardays,k.onvacationdays,k.mailing_address,k.contact,k.contact_phone" +
                    " from kam_member k " +
                    "left join head_portrait h " +
                    "on k.head_portrait_ID = h.head_portrait_id " +
                    "left join province_info p " +
                    "on p.PROVINCE_ID = k.PROVINCE_ID " +
                    "left join city_info c " +
                    "on c.CITY_ID = k.CITY_ID " +
                    "left join organization o " +
                    "on o.organization_id = k.organization_id " +
                    "where k.nickname = '" + member_name + "' and k.member_password = '" + password + "'";

            seresult = this.getReportingTo(sql);

            String member_id = "";

            //构建返回对象
            for (int i = 0; i < seresult.size(); i++) {
                System.out.println(seresult.get(i)[0]);
                KamMemberShowDTO news = new KamMemberShowDTO();
                member_id = seresult.get(i)[1];
                news.setMember_id(seresult.get(i)[1]);
                news.setMember_code(seresult.get(i)[2]);
                news.setNickname(seresult.get(i)[3]);
                news.setMember_name(seresult.get(i)[4]);
                news.setMember_password(seresult.get(i)[5]);
                news.setMember_gender(seresult.get(i)[6]);
                news.setMember_birthday(seresult.get(i)[7]);
                news.setMember_phone(seresult.get(i)[8]);
                news.setMember_id_card(seresult.get(i)[9]);
                news.setProvince_id(seresult.get(i)[10]);
                news.setProvince_name(seresult.get(i)[11]);
                news.setCity_id(seresult.get(i)[12]);
                news.setCity_name(seresult.get(i)[13]);
                news.setOrganization_id(seresult.get(i)[14]);
                news.setOrganization_name(seresult.get(i)[15]);
                news.setHeadimage_url(seresult.get(i)[16]);
                news.setRegister_date(seresult.get(i)[17]);
                news.setUp_date(seresult.get(i)[18]);
                news.setMember_email(seresult.get(i)[19]);
                news.setVacationyeardays(seresult.get(i)[20]);
                news.setOnvacationdays(seresult.get(i)[21]);
                news.setMailing_address(seresult.get(i)[22]);
                news.setContact(seresult.get(i)[23]);
                news.setContact_phone(seresult.get(i)[24]);

                ResultDTO roleList = null;
                roleList = sysRoleMemberResource.getUserRoleById(seresult.get(i)[1]);
                news.setSysrolelsit(roleList.getValues());

                result.add(news);
            }
            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.get_success);

                //登录成功添加日志
                OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();

                operatorlogPullDTO.setOperator_log_type("登录");
                operatorlogPullDTO.setOperator_log_title("用户登录");
                operatorlogPullDTO.setOperator_log_content("OA登录成功");
                operatorlogPullDTO.setMember_id(member_id);
                operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion("密码错误");
            }
        }else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("用户不存在");
        }
        return resultDTO;
    }


    /**
     * 获取用户列表信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getUserListInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json_search = new JSONObject(search);

            String existName = "0";
            String sql_w = "";

            if (json_search.has("organization_id")) {
                sql_w += " AND k.organization_id='" + json_search.getString("organization_id") + "'";
            }

            if (json_search.has("member_id")) {
                sql_w += " AND k.member_id = '" + json_search.getString("member_id") + "'";
            }

                if (json_search.has("userId")) {
                sql_w += " AND k.userId = '" + json_search.getString("userId") + "'";
            }

            String sqlexit = "select count(1) from kam_member as k where 1=1 " + sql_w;


            existName = getReportingToOne(sqlexit);

            List<String[]> seresult = new ArrayList<String[]>();

            List<KamMemberShowDTO> result = new ArrayList<KamMemberShowDTO>();


            if (!existName.equals("0")) {

                //登录sql语句
                String sql = "select k.member_email, k.member_id,k.member_code,k.nickname,k.member_name,k.member_password,k.member_gender," +
                        "k.member_birthday,k.member_phone,k.member_id_card,k.PROVINCE_ID,p.PROVINCE_NAME," +
                        "k.CITY_ID,c.CITY_NAME,k.organization_id,o.organization_name,h.head_portrait_add,k.REGISTER_DATE,k.UP_DATE,k.member_email_psw," +
                        "k.vacationyeardays,k.onvacationdays,k.attachment_id,a.attachment_addr,k.mailing_address,k.contact,k.contact_phone" +
                        " from kam_member k " +
                        "left join head_portrait h " +
                        "on k.head_portrait_ID = h.head_portrait_id " +
                        "left join province_info p " +
                        "on p.PROVINCE_ID = k.PROVINCE_ID " +
                        "left join city_info c " +
                        "on c.CITY_ID = k.CITY_ID " +
                        "left join organization o " +
                        "on o.organization_id = k.organization_id " +
                        "left join attachment a " +
                        "on k.attachment_id = a.attachment_id " +
                        "WHERE 1=1" + sql_w;

                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {
                    System.out.println(seresult.get(i)[0]);
                    KamMemberShowDTO news = new KamMemberShowDTO();
                    news.setMember_email(seresult.get(i)[1]);
                    news.setMember_id(seresult.get(i)[2]);
                    news.setMember_code(seresult.get(i)[3]);
                    news.setNickname(seresult.get(i)[4]);
                    news.setMember_name(seresult.get(i)[5]);
                    news.setMember_password(seresult.get(i)[6]);
                    news.setMember_gender(seresult.get(i)[7]);
                    news.setMember_birthday(seresult.get(i)[8]);
                    news.setMember_phone(seresult.get(i)[9]);
                    news.setMember_id_card(seresult.get(i)[10]);
                    news.setProvince_id(seresult.get(i)[11]);
                    news.setProvince_name(seresult.get(i)[12]);
                    news.setCity_id(seresult.get(i)[13]);
                    news.setCity_name(seresult.get(i)[14]);
                    news.setOrganization_id(seresult.get(i)[15]);
                    news.setOrganization_name(seresult.get(i)[16]);
                    news.setHeadimage_url(seresult.get(i)[17]);
                    news.setRegister_date(seresult.get(i)[18]);
                    news.setUp_date(seresult.get(i)[19]);
                    news.setMember_email_psw(seresult.get(i)[20]);
                    news.setVacationyeardays(seresult.get(i)[21] == "" ? "0" : seresult.get(i)[21]);
                    news.setOnvacationdays(seresult.get(i)[22]);
                    news.setAttachment_id(seresult.get(i)[23]);
                    news.setAttachment_addr(seresult.get(i)[24]);
                    news.setMailing_address(seresult.get(i)[25]);
                    news.setContact(seresult.get(i)[26]);
                    news.setContact_phone(seresult.get(i)[27]);

                    result.add(news);
                }
            }


//            if(!existName.equals("0")){
//
//                //登录sql语句
//                String sql = "SELECT member_id,member_code,member_name,nickname,member_password,member_gender,member_birthday,member_phone,member_id_card,province_id," +
//                        "city_id,organization_id,register_date,up_date FROM kam_member WHERE 1=1" +sql_w + " limit "+page+","+size;
//
//                seresult = this.getReportingTo(sql);
//
//                //构建返回对象
//                for (int i = 0; i < seresult.size(); i++){
//
//                    KamMemberShowDTO km = new KamMemberShowDTO();
//                    km.setMember_id(seresult.get(i)[1]);
//                    km.setMember_code(seresult.get(i)[2]);
//                    km.setMember_name(seresult.get(i)[3]);
//                    km.setNickname(seresult.get(i)[4]);
//                    km.setMember_password(seresult.get(i)[5]);
//                    km.setMember_gender(seresult.get(i)[6]);
//                    km.setMember_birthday(seresult.get(i)[7]);
//                    km.setMember_phone(seresult.get(i)[8]);
//                    km.setMember_id_card(seresult.get(i)[9]);
//                    km.setProvince_id(seresult.get(i)[10]);
//                    km.setCity_id(seresult.get(i)[11]);
//                    km.setOrganization_id(seresult.get(i)[12]);
//                    km.setRegister_date(seresult.get(i)[13]);
//                    km.setUp_date(seresult.get(i)[14]);
//
//                    result.add(km);
//                }
//            }

            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    /**
     * 获取所有用户
     */
    @Override
    public ResultDTO getAllUserInfo() {

        ResultDTO resultDTO = new ResultDTO();

        List<String[]> seresult = new ArrayList<String[]>();

        List<KamMemberShowDTO> result = new ArrayList<KamMemberShowDTO>();

        String sql = "select k.member_email, k.member_id,k.member_code,k.nickname,k.member_name,k.member_password,k.member_gender," +
                "k.member_birthday,k.member_phone,k.member_id_card,k.PROVINCE_ID,p.PROVINCE_NAME," +
                "k.CITY_ID,c.CITY_NAME,k.organization_id,o.organization_name,h.head_portrait_add,k.REGISTER_DATE,k.UP_DATE,k.member_email_psw," +
                "k.vacationyeardays,k.onvacationdays,k.attachment_id,k.mailing_address,k.contact,k.contact_phone" +
                " from kam_member k " +
                "left join head_portrait h " +
                "on k.head_portrait_ID = h.head_portrait_id " +
                "left join province_info p " +
                "on p.PROVINCE_ID = k.PROVINCE_ID " +
                "left join city_info c " +
                "on c.CITY_ID = k.CITY_ID " +
                "left join organization o " +
                "on o.organization_id = k.organization_id ";

        seresult = this.getReportingTo(sql);

        //构建返回对象
        for (int i = 0; i < seresult.size(); i++) {
            System.out.println(seresult.get(i)[0]);
            KamMemberShowDTO news = new KamMemberShowDTO();
            news.setMember_email(seresult.get(i)[1]);
            news.setMember_id(seresult.get(i)[2]);
            news.setMember_code(seresult.get(i)[3]);
            news.setNickname(seresult.get(i)[4]);
            news.setMember_name(seresult.get(i)[5]);
            news.setMember_password(seresult.get(i)[6]);
            news.setMember_gender(seresult.get(i)[7]);
            news.setMember_birthday(seresult.get(i)[8]);
            news.setMember_phone(seresult.get(i)[9]);
            news.setMember_id_card(seresult.get(i)[10]);
            news.setProvince_id(seresult.get(i)[11]);
            news.setProvince_name(seresult.get(i)[12]);
            news.setCity_id(seresult.get(i)[13]);
            news.setCity_name(seresult.get(i)[14]);
            news.setOrganization_id(seresult.get(i)[15]);
            news.setOrganization_name(seresult.get(i)[16]);
            news.setHeadimage_url(seresult.get(i)[17]);
            news.setRegister_date(seresult.get(i)[18]);
            news.setUp_date(seresult.get(i)[19]);
            news.setMember_email_psw(seresult.get(i)[20]);
            news.setVacationyeardays(seresult.get(i)[21] == "" ? "0" : seresult.get(i)[21]);
            news.setOnvacationdays(seresult.get(i)[22]);
            news.setAttachment_id(seresult.get(i)[23]);
            news.setMailing_address(seresult.get(i)[24]);
            news.setContact(seresult.get(i)[25]);
            news.setContact_phone(seresult.get(i)[26]);

            result.add(news);
        }
        if (seresult.size() != 0) {
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.get_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.get_fail);
        }
        return resultDTO;
    }

    //根据用户单个id删除单个用户信息
    @Override
    public ResultDTO deleteKamMemberInfo(KamMemberPullDTO kamMemberPullDTO) {

        String sql = "select count(1) from kam_member where member_id='" + kamMemberPullDTO.getMember_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "delete from kam_member where member_id='" + kamMemberPullDTO.getMember_id() + "'";

            result = this.addReportingTo(QUERY);


        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

    /**
     * 通过member_id修改用户信息
     *
     * @param kamMemberPullDTO
     * @return
     */
    @Override
    public ResultDTO updateKamMemberInfo(KamMemberPullDTO kamMemberPullDTO) {

        String member_id = "select count(1) from kam_member WHERE member_id='" + kamMemberPullDTO.getMember_id() + "'";

        String existName = getReportingToOne(member_id);

        int resultls = 0;

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();
        if (!existName.equals("0")) {
            String QUERY = "";
            QUERY += "update kam_member set register_date='" + time + "'";
            if (null != kamMemberPullDTO.getMember_name() && !"".equals(kamMemberPullDTO.getMember_name())) {
                QUERY += ",member_name='" + kamMemberPullDTO.getMember_name() + "'";
            }

            if (null != kamMemberPullDTO.getNickname() && !"".equals(kamMemberPullDTO.getNickname())) {
                QUERY += ",nickname='" + kamMemberPullDTO.getNickname() + "'";
            }
            if (null != kamMemberPullDTO.getMember_password() && !"".equals(kamMemberPullDTO.getMember_password())) {
                QUERY += ",member_password='" + kamMemberPullDTO.getMember_password() + "'";
            }
//            if(null!=kamMemberPullDTO.getMember_gender() && !"".equals(kamMemberPullDTO.getMember_gender())){
//                QUERY+=",member_gender='"+kamMemberPullDTO.getMember_gender()+"'" ;
//            }
            if (null != kamMemberPullDTO.getMember_phone() && !"".equals(kamMemberPullDTO.getMember_phone())) {
                QUERY += ",member_phone='" + kamMemberPullDTO.getMember_phone() + "'";
            }
            if (null != kamMemberPullDTO.getMember_id_card() && !"".equals(kamMemberPullDTO.getMember_id_card())) {
                QUERY += ",member_id_card='" + kamMemberPullDTO.getMember_id_card() + "'";
            }

            if (null != kamMemberPullDTO.getMember_email() && !"".equals(kamMemberPullDTO.getMember_email())) {
                QUERY += ",member_email='" + kamMemberPullDTO.getMember_email() + "'";
            }
            if (null != kamMemberPullDTO.getWork_time() && !"".equals(kamMemberPullDTO.getWork_time())) {
                QUERY += ",work_time='" + kamMemberPullDTO.getWork_time() + "'";
            }
            if (null != kamMemberPullDTO.getProvince_id() && !"".equals(kamMemberPullDTO.getProvince_id())) {
                QUERY += ",province_id='" + kamMemberPullDTO.getProvince_id() + "'";
            }
            if (null != kamMemberPullDTO.getCity_id() && !"".equals(kamMemberPullDTO.getCity_id())) {
                QUERY += ",city_id='" + kamMemberPullDTO.getCity_id() + "'";
            }
            if (null != kamMemberPullDTO.getHead_image() && !"".equals(kamMemberPullDTO.getHead_image())) {
                QUERY += ",head_image='" + kamMemberPullDTO.getHead_image() + "'";
            }
            if (null != kamMemberPullDTO.getOrganization_id() && !"".equals(kamMemberPullDTO.getOrganization_id())) {
                QUERY += ",organization_id='" + kamMemberPullDTO.getOrganization_id() + "'";
            }
            if (null != kamMemberPullDTO.getMember_email_psw() && !"".equals(kamMemberPullDTO.getMember_email_psw())) {
                QUERY += ",member_email_psw='" + kamMemberPullDTO.getMember_email_psw() + "'";
            }
            if (null != kamMemberPullDTO.getVacationyeardays() && !"".equals(kamMemberPullDTO.getVacationyeardays())) {
                QUERY += ",vacationyeardays='" + kamMemberPullDTO.getVacationyeardays() + "'";
            }
            if (null != kamMemberPullDTO.getOnvacationdays() && !"".equals(kamMemberPullDTO.getOnvacationdays())) {
                QUERY += ",onvacationdays='" + kamMemberPullDTO.getOnvacationdays() + "'";
            }
            if (null != kamMemberPullDTO.getMailing_address() && !"".equals(kamMemberPullDTO.getMailing_address())) {
                QUERY += ",mailing_address='" + kamMemberPullDTO.getMailing_address() + "'";
            }
            if (null != kamMemberPullDTO.getContact() && !"".equals(kamMemberPullDTO.getContact())) {
                QUERY += ",contact='" + kamMemberPullDTO.getContact() + "'";
            }
            if (null != kamMemberPullDTO.getContact_phone() && !"".equals(kamMemberPullDTO.getContact_phone())) {
                QUERY += ",contact_phone='" + kamMemberPullDTO.getContact_phone() + "'";
            }

            if (null != kamMemberPullDTO.getAttachment_id() && !"".equals(kamMemberPullDTO.getAttachment_id()) && null != kamMemberPullDTO.getFilestream() && !"".equals(kamMemberPullDTO.getFilestream())) {
                //调用修改文件方法
                updateImage(kamMemberPullDTO);
            } else if (null != kamMemberPullDTO.getFilestream() && !"".equals(kamMemberPullDTO.getFilestream())) {
                String uid = getImage(kamMemberPullDTO);
                QUERY += ",attachment_id='" + uid + "'";

            }


            QUERY += " where member_id='" + kamMemberPullDTO.getMember_id() + "'";

            resultls = addReportingTo(QUERY);
        }


        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }

        return resultDTO;
    }


    @Override
    public List<KamMemberIdName> getAllMemberIdName() {

        List<KamMemberIdName> listMember = new ArrayList<KamMemberIdName>();

        String sql = "SELECT  member_id,member_name FROM kam_member ";

        List<String[]> seresult = new ArrayList<String[]>();

        seresult = this.getReportingTo(sql);

        for(int i = 0;i<seresult.size();i++){
            KamMemberIdName kamMemberIdName = new KamMemberIdName();
            kamMemberIdName.setMember_id(seresult.get(i)[1]);
            kamMemberIdName.setMember_name(seresult.get(i)[2]);

            listMember.add(kamMemberIdName);
        }

        return listMember;
    }


    //用户注册
    @Override
    public ResultDTO addKamMember(KamMemberPullDTO kamMemberPullDTO) {

        String ORG_MEMBER = "";
        ResultDTO resultDTO = new ResultDTO();
        try {
            //构建返回对象


            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

            //判读用户电话是否存在
            String sql1 = "select count(1) from kam_member where member_phone = '" + kamMemberPullDTO.getMember_phone() + "' ";

            String existMember = getReportingToOne(sql1);

            String idcard = "";


            int resultls = 0;

            if (!existMember.equals("1")) {

                //构建插入sql
                ORG_MEMBER += "insert into kam_member(member_id,member_name,nickname,member_password,member_gender," +
                        "member_phone,member_id_card,register_date,member_email,work_time,province_id,city_id,organization_id,vacationyeardays) VALUES('" +
                        uuid + "','" + kamMemberPullDTO.getMember_name() + "','" + kamMemberPullDTO.getNickname() + "','" +
                        kamMemberPullDTO.getMember_password() + "','" + kamMemberPullDTO.getMember_gender() + "','" +
                        kamMemberPullDTO.getMember_phone() + "','" + idcard + "','" + df.format(new Date()) + "','" +
                        kamMemberPullDTO.getMember_email() + "','" + kamMemberPullDTO.getWork_time() + "','" +
                        kamMemberPullDTO.getProvince_id() + "','" + kamMemberPullDTO.getCity_id() + "','" + kamMemberPullDTO.getOrganization_id()
                        + "','0')";

                //插入业务表
                resultls = addReportingTo(ORG_MEMBER);
            }


            if (resultls != 0) {
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.add_success);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion(warnSrc.add_fail);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    //    /**
//     * 多级查询，返回菜单信息
//     * 从人员—角色—权限—菜单
//     * 一一返回出来
//     *
//     * @param apply
//     * @param page
//     * @param size
//     * @return
//     */
//    @Override
//    public ResultDTO getUserMenuInfo(String apply, String page, String size) {
//
//        ResultDTO resultDTO = new ResultDTO();
//        try {
//            JSONObject json_search = new JSONObject(apply);
//
//            String existName = "0";
//            String sql_w = "";
//
//            if (json_search.has("organization_id")) {
//                sql_w += " AND organization_id='" + json_search.getString("organization_id") + "'";
//            }
//
//            if (json_search.has("member_id")) {
//                sql_w += " AND member_id = '" + json_search.getString("member_id") + "'";
//            }
//
//            String sqlexit = "select count(1) from member_role where 1=1 ";
//
//            String sqlexit_ok = sqlexit + sql_w;
//            existName = getReportingToOne(sqlexit_ok);
//
//
//            List<String[]> seresult = null;
//            List<KamMemberShowDTO> result = null;
//            if (!existName.equals("0")) {
//
//                seresult = new ArrayList<String[]>();
//
//                result = new ArrayList<KamMemberShowDTO>();
//
//                //登录sql语句
//                String sql = "SELECT member_role_id,member_id,memeber_role_create,member_role_create_time,member_role_modify,member_role_modify_time,sys_role_id FROM member_role WHERE 1=1" + sql_w + " limit " + page + "," + size;
//
//                seresult = this.getReportingTo(sql);
//
//                //构建返回对象
//                for (int i = 0; i < seresult.size(); i++) {
//
//                    KamMemberShowDTO km = new KamMemberShowDTO();
//                    km.setSys_role_id(seresult.get(i)[7]);
//
////用member_id拿到sys_role-id的值，去查privailege的值
//                    if (seresult.get(i)[7] != null) {
//
//                        List<String[]> menu = new ArrayList<String[]>();
//
//                        List<KamMemberShowDTO> result1 = new ArrayList<KamMemberShowDTO>();
//
//                        String sys_role_id = seresult.get(i)[7];
//
//                        String sql2 = "select role_privilege_id,role_id,privilege_id,role_privilege_create,role_privilege_create_time,role_privilege_modify,role_privilege_modify_time from role_privilege where role_id='" + sys_role_id + "'";
//
//                        seresult = this.getReportingTo(sql2);
//
//                        for (int j = 0; j < seresult.size(); j++) {
//
//                            KamMemberShowDTO kamMemberShowDTO = new KamMemberShowDTO();
//                            kamMemberShowDTO.setPrivilege_id(seresult.get(j)[3]);
//
//                            if (json_search.has("member_id")) {
//                                //获取该人员权限下的列表
//
//                                String privilege_id = seresult.get(j)[3];
//
//                                ResultDTO menuList;
//
//                                menuList = privilegemenuResource.getPrivilegemenuInfo("{privilege_id:" + seresult.get(j)[3] + "}", "1", "100");
//
//                                kamMemberShowDTO.setSysMenu(menuList.getValues());
//                                result.add(kamMemberShowDTO);
//                            }
//
////                 if (seresult.get(j)[3]!=null){
////                     List<String []> menu1 = new ArrayList<String []>();
////
////                     List<KamMemberShowDTO> result2 = new ArrayList<KamMemberShowDTO>();
////
////                     String privilege_id = seresult.get(j)[3];
//// //拿到privege的值，查询sys_menu_id值
////
////                     String sql3 = "select privilege_menu_id,privilege_id,sys_menu_id,sys_menu_create,sys_menu_create_time,sys_menu_modify,sys_menu_modify_time,privilege_menu_note from privilege_menu where privilege_id ='"+privilege_id+"'";
////
////                     seresult = this.getReportingTo(sql3);
////
////
////                     for(int y = 0; y < seresult.size(); y++){
////
////                         KamMemberShowDTO kamMemberShowDTO1 = new KamMemberShowDTO();
////
////                         kamMemberShowDTO1.setSys_menu_id(seresult.get(y)[2]);
////                         privilegemenuResource.getPrivilegemenuInfo("{privilege_id:"+seresult.get(y)[3]+"}","1","20");
////
////                     }
////
////                 }
//
//                        }
//                    }
//
//                }
//            }
//
//            if (result != null && result.size() != 0) {
//                resultDTO.setStatus("90000");//
//                resultDTO.setDescritpion("用户列表信息调用成功");
//
//
//            } else {
//                resultDTO.setStatus("90001");//
//                resultDTO.setDescritpion("用户列表信息调用失败");
//            }
//            resultDTO.setValues(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return resultDTO;
//    }
    @Override
    public ResultDTO getUserMenuInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        List<KamMemberShowDTO> result = null;
        try {
            JSONObject json_search = new JSONObject(search);

            String existName = "0";
            String sql_w = "";
            if (json_search.has("member_id")) {

                result = new ArrayList<KamMemberShowDTO>();
                //获取该人员权限下的列表
                KamMemberShowDTO kamMemberShowDTO = new KamMemberShowDTO();

                String sql = "SELECT DISTINCT(sm.sys_menu_id),sm.sys_menu_name,sm.sys_menu_url,sm.sys_menu_create,sm.sys_menu_create_time," +
                        "sm.sys_menu_modify,sm.sys_menu_modify_time,sm.parent_sys_menu_id,sm.menu_order,a.attachment_id," +
                        "a.attachment_addr FROM sys_role_menu srm " +
                        "LEFT JOIN sys_menu sm " +
                        "ON srm.sys_menu_id = sm.sys_menu_id " +
                        "LEFT JOIN sys_role_member sr " +
                        "ON sr.sys_role_id = srm.sys_role_id " +
                        "LEFT JOIN attachment a " +
                        "ON a.attachment_id = sm.attachment_id " +
                        "WHERE 1=1 " +
                        "and (sm.parent_sys_menu_id = '' || sm.parent_sys_menu_id is NULL ) " +
                        "and sm.sys_menu_id IS NOT NULL " +
                        "and sr.member_id = '" + json_search.get("member_id").toString() + "' ";
                List<String[]> seresult = new ArrayList<String[]>();
                seresult = this.getReportingTo(sql);

                List<SysmenuShowDTO> menuResult = new ArrayList<SysmenuShowDTO>();

                for (int i=0;i<seresult.size();i++){
                    SysmenuShowDTO sysmenuShowDTO = new SysmenuShowDTO();
                    sysmenuShowDTO.setSys_menu_id(seresult.get(i)[1]);
                    sysmenuShowDTO.setSys_menu_name(seresult.get(i)[2]);
                    sysmenuShowDTO.setSys_menu_url(seresult.get(i)[3]);
                    sysmenuShowDTO.setSys_menu_create(seresult.get(i)[4]);
                    sysmenuShowDTO.setSys_menu_create_time(seresult.get(i)[5]);
                    sysmenuShowDTO.setSys_menu_modify(seresult.get(i)[6]);
                    sysmenuShowDTO.setSys_menu_modify_time(seresult.get(i)[7]);
                    sysmenuShowDTO.setParent_sys_menu_id(seresult.get(i)[8]);
                    sysmenuShowDTO.setMenu_order(seresult.get(i)[9]);
                    sysmenuShowDTO.setAttachment_id(seresult.get(i)[10]);
                    sysmenuShowDTO.setImageAddress(seresult.get(i)[11] == null?"":seresult.get(i)[11]);

                    //主菜单图标
                    if (!seresult.get(i)[10].equals("") && seresult.get(i)[10] != null) {
                        ResultDTO attach = attachmentResource.getAttachmentInfo("{attachment_id:" + seresult.get(i)[10] + "}", "1", "1");
                        sysmenuShowDTO.setImages(attach.getValues());
                    }

                    List<String[]> sonList = new ArrayList<String[]>();
                    //根据parent_id获取子菜单
                    String parent_sql = "SELECT DISTINCT(sm.sys_menu_id),sm.sys_menu_name,sm.sys_menu_url,sm.sys_menu_create, " +
                            "sm.sys_menu_create_time,sm.sys_menu_modify,sm.sys_menu_modify_time " +
                            ",sm.parent_sys_menu_id,sm.menu_order FROM sys_role_menu srm " +
                            "LEFT JOIN sys_menu sm " +
                            "ON srm.sys_menu_id = sm.sys_menu_id  " +
                            "LEFT JOIN sys_role_member sr " +
                            "ON sr.sys_role_id = srm.sys_role_id " +
                            "LEFT JOIN attachment a " +
                            "ON a.attachment_id = sm.attachment_id " +
                            "WHERE 1=1 " +
                            "AND (sm.parent_sys_menu_id <> '' || sm.parent_sys_menu_id IS NOT NULL ) " +
                            "AND sr.member_id = '" + json_search.get("member_id").toString() + "'" +
                            "and parent_sys_menu_id='" + seresult.get(i)[1] + "' ORDER BY sm.sys_menu_create_time";

                    List<SysmenuShowDTO> user = new ArrayList<SysmenuShowDTO>();

                    sonList = this.getReportingTo(parent_sql);
                    for(int j=0;j<sonList.size();j++){
                        SysmenuShowDTO sonSysmenuShowDTO = new SysmenuShowDTO();
                        sonSysmenuShowDTO.setSys_menu_id(sonList.get(j)[1]);
                        sonSysmenuShowDTO.setSys_menu_name(sonList.get(j)[2]);
                        sonSysmenuShowDTO.setSys_menu_url(sonList.get(j)[3]);
                        sonSysmenuShowDTO.setSys_menu_create(sonList.get(j)[4]);
                        sonSysmenuShowDTO.setSys_menu_create_time(sonList.get(j)[5]);
                        sonSysmenuShowDTO.setSys_menu_modify(sonList.get(j)[6]);
                        sonSysmenuShowDTO.setSys_menu_modify_time(sonList.get(j)[7]);
                        sonSysmenuShowDTO.setParent_sys_menu_id(sonList.get(j)[8]);
                        sonSysmenuShowDTO.setMenu_order(sonList.get(j)[9]);
                        user.add(sonSysmenuShowDTO);
                    }

                    sysmenuShowDTO.setUser(user);

                    menuResult.add(sysmenuShowDTO);
                }

                kamMemberShowDTO.setSysMenu(menuResult);

                result.add(kamMemberShowDTO);
            }
            if (result != null && result.size() != 0) {
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    //   @Override
//    public ResultDTO updateKamMember(KamMemberPullDTO kamMemberPullDTO) {
//        return null;
//    }
    public String getImage(KamMemberPullDTO kamMemberPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/menu";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + uuid + ".png";


        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(kamMemberPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/menu/" + uuid + ".png";

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //插入头像表
            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,file_type,file_name) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','" + uuid + ".png" + "','" + uuid + ".png" + "')";
            // enpenform_image += "insert into file(file_id,file_addr,file_time,filegui_id,file_name) values ('" + uuid + "','" +imgurl+ "','" +df.format(new Date()) + "','" + dataDTO.getFilegui_id() + "','" + dataDTO.getFiletype() + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);
        }

        if (resultls != 0) {
            return uuid;
        }
        return null;
    }

    public String updateImage(KamMemberPullDTO kamMemberPullDTO) {
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/menu";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + uuid + ".png";

        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(kamMemberPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = image_pathSerAddr + "/menu/" + uuid + ".png";

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //修改头像表
            enpenform_image += "update attachment set attachment_addr='" + imgurl + "',head_portrait_time = '" + df.format(new Date()) + "',file_type = '" + uuid + ".png" + "',file_name = '" + uuid + ".png" + "' where attachment_id='" + kamMemberPullDTO.getAttachment_id() + "'";
            // enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);
        }

        if (resultls != 0) {
            return uuid;
        }

        return null;
    }

}
